import { getAllClientCodeApi } from "@/api/common";
import { computed, reactive, ref } from "vue";

export default function InitHook() {
  const formState = reactive<Partial<RoleAndPermission.AddForm>>({
    name: undefined,
    remark: undefined,
    dataScope: undefined,
    type: "1",
    clients: []
  });
  const formDataRef = ref<any>(null);
  const loading = ref<boolean>(false);
  const isInit = ref<boolean>(false);
  const clientOpts = ref<Array<Base.GeneraDataType>>([]);
  const showPanel = computed<string>(() => {
    if (formState.type === "1") return "management";
    else return "management";
  });
  const activeMenu = computed<Base.GeneraDataType>(() => {
    return clientOpts.value.find((t: Base.GeneraDataType) => t.code === formState.type) || {};
  });
  const getAllClientCode = () => {
    return getAllClientCodeApi().then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        clientOpts.value = data.map(({ name, code }: any) => {
          return {
            code,
            name,
            checked: false,
            clients: []
          };
        });
      }
    });
  };
  return {
    formState,
    formDataRef,
    loading,
    isInit,
    clientOpts,
    showPanel,
    getAllClientCode,
    activeMenu
  };
}
